| Conditions | 1 |
| Paths | 1 |
| Total Lines | 177 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /* |
||
| 8 | describe("A Grid", function () { |
||
| 9 | |||
| 10 | var Book = Backbone.Model.extend({}); |
||
| 11 | var Books = Backbone.Collection.extend({ |
||
| 12 | model: Book |
||
| 13 | }); |
||
| 14 | |||
| 15 | var books; |
||
| 16 | var grid; |
||
| 17 | beforeEach(function () { |
||
| 18 | books = new Books([{ |
||
| 19 | id: 1, |
||
| 20 | title: "Alice's Adventures in Wonderland" |
||
| 21 | }, { |
||
| 22 | id: 2, |
||
| 23 | title: "A Tale of Two Cities" |
||
| 24 | }, { |
||
| 25 | id: 3, |
||
| 26 | title: "The Catcher in the Rye" |
||
| 27 | }]); |
||
| 28 | |||
| 29 | grid = new Backgrid.Grid({ |
||
| 30 | columns: [{ |
||
| 31 | name: "title", |
||
| 32 | cell: "string" |
||
| 33 | }], |
||
| 34 | collection: books, |
||
| 35 | footer: Backgrid.Footer |
||
| 36 | }); |
||
| 37 | }); |
||
| 38 | |||
| 39 | it("renders a table with a body, optional header, and an optional footer section", function () { |
||
| 40 | spyOn(grid, "trigger"); |
||
| 41 | spyOn(grid.header, "render").and.callThrough(); |
||
| 42 | spyOn(grid.footer, "render").and.callThrough(); |
||
| 43 | spyOn(grid.body, "render").and.callThrough(); |
||
| 44 | |||
| 45 | grid.render(); |
||
| 46 | |||
| 47 | expect(grid.el.tagName).toBe("TABLE"); |
||
| 48 | expect(grid.header.render.calls.count()).toBe(1); |
||
| 49 | expect(grid.footer.render.calls.count()).toBe(1); |
||
| 50 | expect(grid.body.render.calls.count()).toBe(1); |
||
| 51 | expect(grid.trigger.calls.count()).toBe(1); |
||
| 52 | expect(grid.trigger).toHaveBeenCalledWith("backgrid:rendered", grid); |
||
| 53 | }); |
||
| 54 | |||
| 55 | it("will render a table with the header, body, footer and row classes supplied in the constructor options", function () { |
||
| 56 | |||
| 57 | var CustomHeader = Backgrid.Header.extend({}); |
||
| 58 | var CustomBody = Backgrid.Body.extend({}); |
||
| 59 | var CustomRow = Backgrid.Row.extend({}); |
||
| 60 | var CustomFooter = Backgrid.Footer.extend({}); |
||
| 61 | |||
| 62 | grid = new Backgrid.Grid({ |
||
| 63 | columns: [{ |
||
| 64 | name: "title", |
||
| 65 | cell: "string" |
||
| 66 | }], |
||
| 67 | collection: books, |
||
| 68 | header: CustomHeader, |
||
| 69 | body: CustomBody, |
||
| 70 | row: CustomRow, |
||
| 71 | footer: CustomFooter, |
||
| 72 | className: "class-name" |
||
| 73 | }); |
||
| 74 | |||
| 75 | grid.render(); |
||
| 76 | |||
| 77 | expect(grid.header instanceof CustomHeader).toBe(true); |
||
| 78 | expect(grid.body instanceof CustomBody).toBe(true); |
||
| 79 | expect(grid.body.rows[0] instanceof CustomRow).toBe(true); |
||
| 80 | expect(grid.footer instanceof CustomFooter).toBe(true); |
||
| 81 | |||
| 82 | expect(grid.header.className).not.toBe("class-name"); |
||
| 83 | expect(grid.body.className).not.toBe("class-name"); |
||
| 84 | expect(grid.body.rows[0].className).not.toBe("class-name"); |
||
| 85 | expect(grid.footer.className).not.toBe("class-name"); |
||
| 86 | }); |
||
| 87 | |||
| 88 | it("will render a table with a caption element", function () { |
||
| 89 | var caption = "Table of data" |
||
| 90 | grid = new Backgrid.Grid({ |
||
| 91 | columns: [{ |
||
| 92 | name: "title", |
||
| 93 | cell: "string" |
||
| 94 | }], |
||
| 95 | collection: books, |
||
| 96 | caption: caption, |
||
| 97 | }); |
||
| 98 | |||
| 99 | grid.render(); |
||
| 100 | |||
| 101 | expect($(grid.el).find("caption")); |
||
| 102 | expect($(grid.el).find("caption").text()).toBe(caption); |
||
| 103 | }); |
||
| 104 | |||
| 105 | it("will clean up all its decendant views when remove is called", function () { |
||
| 106 | expect(grid.remove().constructor).toBe(Backgrid.Grid); |
||
| 107 | }); |
||
| 108 | |||
| 109 | it("will delegate insertRow, removeRow and sort to the body", function () { |
||
| 110 | spyOn(grid.body, "insertRow").and.callThrough(); |
||
| 111 | spyOn(grid.body, "removeRow").and.callThrough(); |
||
| 112 | spyOn(grid.body, "sort").and.callThrough(); |
||
| 113 | grid.insertRow({}); |
||
| 114 | expect(grid.body.insertRow).toHaveBeenCalledWith({}); |
||
| 115 | var last = grid.collection.last(); |
||
| 116 | grid.removeRow(last); |
||
| 117 | expect(grid.body.removeRow).toHaveBeenCalledWith(last); |
||
| 118 | grid.sort("title", "descending"); |
||
| 119 | expect(grid.body.sort).toHaveBeenCalledWith("title", "descending"); |
||
| 120 | }); |
||
| 121 | |||
| 122 | it("will delegate to columns.add and columns.remove from insertColumn and removeColumn", function () { |
||
| 123 | spyOn(grid.columns, "add").and.callThrough(); |
||
| 124 | spyOn(grid.columns, "remove").and.callThrough(); |
||
| 125 | grid.insertColumn({ |
||
| 126 | name: "id", |
||
| 127 | cell: "integer" |
||
| 128 | }); |
||
| 129 | expect(grid.columns.add).toHaveBeenCalledWith({ |
||
| 130 | name: "id", |
||
| 131 | cell: "integer" |
||
| 132 | }); |
||
| 133 | var col = grid.columns.last(); |
||
| 134 | grid.removeColumn(col); |
||
| 135 | expect(grid.columns.remove).toHaveBeenCalledWith(col); |
||
| 136 | }); |
||
| 137 | |||
| 138 | it("will refresh on columns reset", function () { |
||
| 139 | grid.render(); |
||
| 140 | grid.columns.reset([{ |
||
| 141 | name: "id", |
||
| 142 | cell: "integer" |
||
| 143 | }]); |
||
| 144 | |||
| 145 | var thead = grid.el.childNodes[0]; |
||
| 146 | expect(thead.tagName == "THEAD").toBe(true); |
||
| 147 | expect($(thead).find("tr").length).toBe(1); |
||
| 148 | expect($(thead).find("tr > th.editable.sortable.renderable.id > button > span.sort-caret").length).toBe(1); |
||
| 149 | expect($(thead).find("tr > th.editable.sortable.renderable.id > button").text()).toBe("id"); |
||
| 150 | |||
| 151 | var tfoot = grid.el.childNodes[1]; |
||
| 152 | expect(tfoot.tagName == "TFOOT").toBe(true); |
||
| 153 | expect(tfoot.childNodes.length).toBe(0); |
||
| 154 | |||
| 155 | var tbody = grid.el.lastChild; |
||
| 156 | expect(tbody.tagName == "TBODY").toBe(true); |
||
| 157 | expect($(tbody).find("tr").length).toBe(3); |
||
| 158 | expect($(tbody).find("tr:nth-child(1) > td.integer-cell.editable.sortable.renderable").length).toBe(1); |
||
| 159 | expect($(tbody).find("tr:nth-child(1) > td.integer-cell.editable.sortable.renderable").text()).toBe("1"); |
||
| 160 | expect($(tbody).find("tr:nth-child(2) > td.integer-cell.editable.sortable.renderable").length).toBe(1); |
||
| 161 | expect($(tbody).find("tr:nth-child(2) > td.integer-cell.editable.sortable.renderable").text()).toBe("2"); |
||
| 162 | expect($(tbody).find("tr:nth-child(3) > td.integer-cell.editable.sortable.renderable").length).toBe(1); |
||
| 163 | expect($(tbody).find("tr:nth-child(3) > td.integer-cell.editable.sortable.renderable").text()).toBe("3"); |
||
| 164 | }); |
||
| 165 | |||
| 166 | it("will inherit the constructor's columns", function () { |
||
| 167 | var columns = [{ |
||
| 168 | name: "title", |
||
| 169 | cell: "string" |
||
| 170 | }]; |
||
| 171 | |||
| 172 | var CustomGrid = Backgrid.Grid.extend({ |
||
| 173 | columns: columns, |
||
| 174 | className: 'backgrid customBackgrid' |
||
| 175 | }); |
||
| 176 | |||
| 177 | var customGrid = new CustomGrid({ |
||
| 178 | collection: books |
||
| 179 | }); |
||
| 180 | |||
| 181 | expect(customGrid.columns.models.length).toBe(1); |
||
| 182 | expect(customGrid.columns.at(0).get("name")).toBe("title"); |
||
| 183 | }); |
||
| 184 | }); |
||
| 185 |